package com.example.zhangwei.ui.demo;


import com.example.zhangwei.Constants;
import com.example.zhangwei.R;
import com.example.zhangwei.broadcastreceiver.MyReceiver;
import com.example.zhangwei.model.DataSerializable;
import com.example.zhangwei.service.MyService;
import com.example.zhangwei.util.BaseActivity;
import com.example.zhangwei.util.MyLogger;
import com.example.zhangwei.util.ToastMaster;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import android.widget.Toast;
public class ActivityLiving2 extends BaseActivity {
	
	private TextView mText2;
	
	private MyLogger log = MyLogger.getLogger("ActivityLiving2");
	
	private DataSerializable parcelableData;
	
	private BroadcastReceiver receiver = new BroadcastReceiver()
	{
		@Override
		public void onReceive(Context context, Intent intent) {
			// TODO Auto-generated method stub
			if (MyReceiver.OPEN_MY_DIALOG.equals(intent.getAction()))
			{
				int key = intent.getIntExtra("key", -1);
				showDialogMessage(ActivityLiving2.this, "提示", "已经弹出广播对话框 接受key为:"+key,
	            		getString(R.string.confirm), getString(R.string.cancel), 2);
			}
			else if (ConnectivityManager.CONNECTIVITY_ACTION.equals(intent.getAction()))//监听网络状态
			{
				
			}
			else if (MyService.SEND_MY_SERVICE_DATA.equals(intent.getAction()))
			{
				DataSerializable data = (DataSerializable) intent.getSerializableExtra(Constants.PassDataKey.SERIALIZABLE);  
				if (data != null)
				{
					String strValue = "";
					strValue += data.id + "|";
					strValue += data.name + "|";
					strValue += data.mapData.get("map") + "|";
					ToastMaster.getInstance().makeText(ActivityLiving2.this, strValue, Toast.LENGTH_LONG);
				}
			}
			else
			{
				ToastMaster.getInstance().makeText(ActivityLiving2.this, "传入的不是MyReceiver.OPEN_MY_DIALOG", Toast.LENGTH_LONG);
			}
			
		}
	};
		
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		if (savedInstanceState != null) {
			String key = savedInstanceState.get("saveKey").toString();
			log.e("savedInstanceState.getkey=="+key);
		}			
		setContentView(R.layout.activity_living2);
		mText2 = (TextView)findViewById(R.id.activity_living_text2);
		//获取Parcelable传递 复杂类
		Intent intent = getIntent();
		parcelableData = (DataSerializable) intent.getSerializableExtra(Constants.PassDataKey.SERIALIZABLE);  
		if (parcelableData != null)
		{
			String strValue = "";
			strValue += parcelableData.id + "\n";
			strValue += parcelableData.name + "\n";
			strValue += parcelableData.mapData.get("map") + "\n";
			mText2.setText(strValue);
		}
				
		findViewById(R.id.activity_living_button).setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				changeTo(ActivityLiving2.class);
			}
		});
		
		findViewById(R.id.activity_living_button2).setVisibility(View.GONE);
		
		findViewById(R.id.activity_living_button_receiver).setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent("android.intent.action.my_broadcast");  
			    intent.putExtra("msg", "ActivityLiving2 send message to MyReceiver");  
			    sendBroadcast(intent); 
			}
		});
		
	    findViewById(R.id.activity_living_button_receiver2).setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(MyReceiver.OPEN_MY_DIALOG);  
			    intent.putExtra("key", 1);  
			    sendBroadcast(intent); 
			}
		});
	    
	    // 注册广播
        IntentFilter filter = new IntentFilter();
        //传入值
        filter.addAction(MyReceiver.OPEN_MY_DIALOG);
        // 网络状态
        filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
        // 注册myservice处理值
        filter.addAction(MyService.SEND_MY_SERVICE_DATA);
        try
        {
            registerReceiver(receiver, filter);
        }
        catch (Exception e)
        {
        	log.e(e.toString());
        }
        
        
        findViewById(R.id.activity_living_service_start).setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				startService(new Intent(ActivityLiving2.this,MyService.class));
			}
		});
        
        findViewById(R.id.activity_living_service_stop).setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				stopService(new Intent(ActivityLiving2.this,MyService.class));
			}
		});
        
        findViewById(R.id.activity_living_service_data).setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(MyService.SEND_MY_SERVICE_DATA_EXECUTE);
				intent.putExtra(Constants.PassDataKey.SERIALIZABLE, parcelableData);
				sendBroadcast(intent);
			}
		});
	}
	
	@Override
	protected void onRestart() {
		// TODO Auto-generated method stub
		super.onRestart();
		log.e("----------onRestart2-----------");
	}
	
	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
		log.e("----------onStart2-----------");
	}
	
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		log.e("----------onResume2-----------");
	}
	
	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		log.e("----------onPause2-----------");
	}
	
	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
		log.e("----------onStop2-----------");
	}
	
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		unregisterReceiver(receiver);  
		super.onDestroy();
		log.e("----------onDestroy2-----------");
	}
	
	@Override
	protected void onSaveInstanceState(Bundle outState) {
		// TODO Auto-generated method stub
		super.onSaveInstanceState(outState);		
		outState.putString("saveKey", "saving");
		log.e("----------onSaveInstanceState2-----------");
	}
	
	

}
